package com.ziq.base.utils

import android.graphics.Color
import android.os.Build
import android.view.View
import android.view.Window
import android.view.WindowInsets
import android.view.WindowInsetsAnimation
import androidx.annotation.ColorInt
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat


class SystemBarUtil {
    companion object {
        //沉浸式状态栏
        fun immerseStatusBar(
            window: Window?,
            @ColorInt statusBarColor: Int = Color.TRANSPARENT,
            statusBarIsLight: Boolean = false,
            @ColorInt navigationBarColor: Int = Color.BLACK,
            navigationBarIsLight: Boolean = false,
        ){
            window?.let { window ->
                //关键代码,沉浸
                WindowCompat.setDecorFitsSystemWindows(window, false)
                //设置专栏栏和导航栏的底色，透明
                window.statusBarColor = statusBarColor
                window.navigationBarColor = navigationBarColor
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                    window.navigationBarDividerColor = Color.TRANSPARENT
                }

                WindowCompat.getInsetsController(window, window.decorView).let {
                    it.isAppearanceLightStatusBars = statusBarIsLight
                    it.isAppearanceLightNavigationBars = navigationBarIsLight
                }
            }
        }
        fun getStatusBarsHeight(view: View, onAction:(height: Int) ->Unit) {
            view.post {
                onAction.invoke(getStatusBarsHeight(view))
            }
        }

        fun getNavigationBarsHeight(view: View, onAction:(height: Int) ->Unit) {
            view.post {
                onAction.invoke(getNavigationBarsHeight(view))
            }
        }
        fun getKeyboardHeight(view: View, onAction:(height: Int) ->Unit) {
            view.post {
                onAction.invoke(getKeyboardHeight(view))
            }
        }

        private fun hasStatusBars(view: View): Boolean {
            val windowInsetsCompat = ViewCompat.getRootWindowInsets(view) ?: return false
            return windowInsetsCompat.isVisible(WindowInsetsCompat.Type.statusBars())
                    && windowInsetsCompat.getInsets(WindowInsetsCompat.Type.statusBars()).top > 0
        }

        private fun getStatusBarsHeight(view: View): Int {
            val windowInsetsCompat = ViewCompat.getRootWindowInsets(view) ?: return 0
            return windowInsetsCompat.getInsets(WindowInsetsCompat.Type.statusBars()).top
        }

        private fun hasNavigationBars(view: View): Boolean {
            val windowInsetsCompat = ViewCompat.getRootWindowInsets(view) ?: return false
            return windowInsetsCompat.isVisible(WindowInsetsCompat.Type.navigationBars())
                    && windowInsetsCompat.getInsets(WindowInsetsCompat.Type.navigationBars()).bottom > 0
        }

        private fun getNavigationBarsHeight(view: View): Int {
            val windowInsetsCompat = ViewCompat.getRootWindowInsets(view) ?: return 0
            return windowInsetsCompat.getInsets(WindowInsetsCompat.Type.navigationBars()).bottom
        }

        private fun getKeyboardHeight(view: View): Int {
            val windowInsetsCompat = ViewCompat.getRootWindowInsets(view) ?: return 0
            return windowInsetsCompat.getInsets(WindowInsetsCompat.Type.ime()).bottom
        }

        fun addSystemBarHeightChangeCallBack(view: View, onAction:(statusBarHeight: Int, navigationBarHeight : Int, keyboardHeight:Int) ->Unit){
            view.post {
                onAction.invoke(getStatusBarsHeight(view), getNavigationBarsHeight(view), getKeyboardHeight(view))
            }

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                val cb = object : WindowInsetsAnimation.Callback(DISPATCH_MODE_STOP) {
                    override fun onProgress(
                        insets: WindowInsets,
                        animations: MutableList<WindowInsetsAnimation>
                    ): WindowInsets {
                        onAction.invoke(
                            insets.getInsets(WindowInsetsCompat.Type.statusBars()).top,
                            insets.getInsets(WindowInsetsCompat.Type.navigationBars()).bottom,
                            insets.getInsets(WindowInsetsCompat.Type.ime()).bottom)
                        return insets
                    }

                    override fun onEnd(animation: WindowInsetsAnimation) {
                        super.onEnd(animation)
                        onAction.invoke(getStatusBarsHeight(view), getNavigationBarsHeight(view), getKeyboardHeight(view))
                    }

                }
                view.setWindowInsetsAnimationCallback(cb)
            } else {
                ViewCompat.setOnApplyWindowInsetsListener(view) { _, insets ->
                    onAction.invoke(
                        insets.getInsets(WindowInsetsCompat.Type.statusBars()).top,
                        insets.getInsets(WindowInsetsCompat.Type.navigationBars()).bottom,
                        insets.getInsets(WindowInsetsCompat.Type.ime()).bottom)
                    insets
                }
            }
        }


    }
}